<template>
    <div>
        <h1>会员增加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">会员编号</td>
                    <td>
                        <input type="text" v-model="info.memberCode" disabled>
                    </td>
                </tr>
                 <tr>
                    <td align="right">手机号</td>
                    <td>
                        <input type="text" v-model="info.tel" maxlength="11">
                    </td>
                </tr>
                 <tr>
                    <td align="right">姓名</td>
                    <td>
                        <input type="text" v-model="info.userName">
                    </td>
                </tr>
                 <tr>
                    <td align="right">身份证号码</td>
                    <td>
                        <input type="text" v-model="info.card" maxlength="18">
                    </td>
                </tr>
                 <tr>
                    <td align="right">会员生日</td>
                    <td>
                        <input type="date" v-model="info.birthDay">
                    </td>
                </tr>
                 <tr>
                    <td align="right">会员积分</td>
                    <td>
                        <input type="text" v-model="info.score">
                    </td>
                </tr>
                 <tr>
                    <td align="right">会员余额</td>
                    <td>
                        <input type="text" v-model="info.money">
                    </td>
                </tr>
                 <tr>
                    <td align="right">会员等级</td>
                    <td>
                        <select v-model="info.levelId">
                            <option value="">请选择</option>
                            <option :value="item.id" v-for="item in lave">{{item.levelName}}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">会员性别</td>
                    <td>
                        <input type="radio" name="sex" value="1" v-model="info.gender">男
                        <input type="radio" name="sex" value="0" v-model="info.gender">女
                    </td>
                </tr>
                <tr>
                    <td align="right">会员爱好</td>
                    <td>
                        <input type="checkbox" name="likes" value="吃" v-model="info.likes">吃
                        <input type="checkbox" name="likes" value="喝" v-model="info.likes">喝
                        <input type="checkbox" name="likes" value="拉" v-model="info.likes">拉
                        <input type="checkbox" name="likes" value="撒" v-model="info.likes">撒
                    </td>
                </tr>
                <tr>
                    <td align="right">会员简介</td>
                    <td>
                        <textarea rows="5" cols="50" v-model="info.desc">

                        </textarea>
                    </td>
                </tr>
                <tr>
                    <td align="right">是否同意条款</td>
                    <td>
                        <input type="checkbox" v-model="info.isagree">xxx条款
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="确定" class="btn btn-success" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import{ref,reactive,onMounted}from 'vue'
import axios from 'axios'
import moment from 'moment'
import {useRouter}from 'vue-router';
const router=useRouter();
//文档就绪
onMounted(()=>{
    getMemberType();
})
//下拉框
let lave:any=ref([]);
const getMemberType=()=>{
    axios({
        url:'https://localhost:7253/api/Member/GetMemberType',
        method:'get'
    })
    .then((res)=>{
        console.log(res);
        lave.value=res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}
//添加
let info:any=reactive({
    "memberCode": moment(new Date()).format('YYYYMMDD')+Math.random().toString().substr(3,3),
    "tel": "",
    "userName": "",
    "card": "",
    "birthDay": "",
    "score": '',
    "money": '',
    "levelId": '',
    "gender": 1,
    "likes": [],
    "desc": "",
    "isagree": '',
})
const ok=()=>{
    if(info.tel==""){
        alert('手机号不能为空')
        return;
    }
    if(info.userName==""){
        alert('姓名不能为空')
        return;
    }
    if(info.desc==""){
        alert('简介不能为空')
        return;
    }
    if(info.isagree==false){
        alert('同意条款必选')
        return;
    }
    info.likes=info.likes.toString();
    axios({
        url:'https://localhost:7253/api/Member/AddMember',
        method:'post',
        data:info
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert('手机号重复')
            return;
        }
        if(res.data>0){
            alert('增加成功')
            router.push('/MemberList')
            return;
        }
        else
        {
            alert('增加失败')
            return;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>